package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;

import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;

import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;

import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;


@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //新增菜品
    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        //通过工具类 复制数据
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //通过先添加套餐 获取套餐主键
        setmealMapper.add(setmeal);
        Long setmealId = setmeal.getId();
        //然后获取 添加到套餐菜品关系表       //传入套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //本来需要判断是否为空 ，有判断就不需要了
//        setmealDishes.forEach(setmealDish -> setmealDish.setDishId(setmealId));
//        setmealDishes.stream().forEach(s->s.setSetmealId(setmealId));
//        setmealDishes.stream().forEach(new Consumer<SetmealDish>() {
//            @Override
//            public void accept(SetmealDish setmealDish) {
//                setmealDish.setSetmealId(setmealId);
//            }
//        });
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //向关系表插
        setmealDishMapper.add(setmealDishes);
    }

    @Override
    public PageResult pageset(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //去空格
        if (setmealPageQueryDTO.getName()!=null){
            setmealPageQueryDTO.setName(setmealPageQueryDTO.getName().trim());
        }


        // 执行分页条件查询
        Page<SetmealVO> list = setmealMapper.list(setmealPageQueryDTO);
        //获取 分页数据
        PageResult pageResult = new PageResult(list.getTotal(),list.getResult());
        //封装数据
        return pageResult;
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //获取套餐
        for (Long id : ids) {
            Setmeal setmeal =  setmealMapper.getById(id);
            //判断是否在售
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (Long id : ids) {
            //删除套餐
            setmealMapper.delete(id);
            //和 套餐关联
            setmealDishMapper.deleteSid(id);
        }
    }

    @Override
    public SetmealVO getId(Long id) {
        //根据id查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        //根据套餐 id查  设计获取菜品id的方法
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishIdsByDishIds(id);

        //复制 将查询de数据封装到vo
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //负责菜品
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //先new 套餐对象复制数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改基本信息
        setmealMapper.update(setmeal);
        //套餐id
        Long  setmealDTOId = setmealDTO.getId();
        //里面有表 的 先删完里面数据 重新插 方便
        setmealDishMapper.deleteSid(setmealDTOId);
        //重新插入
        List<SetmealDish> dishIdsByDishIds = setmealDTO.getSetmealDishes();

        //循环id
        for (SetmealDish dishIdsByDishId : dishIdsByDishIds) {
            dishIdsByDishId.setSetmealId(setmealDTO.getId());
        }
        //重新插入
        setmealDishMapper.add(dishIdsByDishIds);

    }

    //修改起售停售状态
    @Override
    public void startorUn(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);

        setmealMapper.update(setmeal);
    }




    /**

     - 条件查询
     - @param setmeal
     - @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.Setmeallist(setmeal);
        return list;
    }

    /**

     - 根据id查询菜品选项
     - @param id
     - @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}


